//
//  ConversationTableViewCell.m
//  MiniWeChat
//
//  Created by ByteDance on 2025/6/18.
//

#import "ConversationTableViewCell.h"
#import "Masonry.h"

@interface ConversationTableViewCell ()

@property(nonatomic,strong)UIImageView *avatarImageView;
@property(nonatomic,strong)UILabel *nicknameLabel;
@property(nonatomic,strong)UILabel *lastMsgLabel;
@property(nonatomic,strong)UILabel *lastMsgTimeLabel;
@property(nonatomic,strong)UILabel *tipLabel;
@property(nonatomic,strong)UILabel *unReadBadge;
@property(nonatomic,strong)UIView  *line;

@end

@implementation ConversationTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        // 头像
        self.avatarImageView = [[UIImageView alloc] init];
        self.avatarImageView.clipsToBounds = YES;
        self.avatarImageView.contentMode = UIViewContentModeScaleAspectFit;
        self.avatarImageView.layer.cornerRadius = 4; // 圆角
        [self.contentView addSubview:self.avatarImageView];
        
        [self.avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.contentView.mas_left).offset(15); // 左
            make.top.equalTo(self.contentView.mas_top).offset(8); // 顶
            make.bottom.equalTo(self.contentView.mas_bottom).offset(-8); // 底
            make.width.height.equalTo(@48); // 宽、高
        }];
        
        // 最新消息时间
        self.lastMsgTimeLabel = [[UILabel alloc] init];
        self.lastMsgTimeLabel.textColor = [UIColor colorWithRed:160.f/256.f green:160.f/256.f blue:160.f/256.f alpha:1.0];
        self.lastMsgTimeLabel.font = [UIFont systemFontOfSize:12.0f];
        [self.contentView addSubview:self.lastMsgTimeLabel];
        [self.lastMsgTimeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.contentView.mas_right).offset(-20);
            make.top.mas_equalTo(self.contentView.mas_top).offset(15);
        }];
        
        [self.lastMsgTimeLabel setContentCompressionResistancePriority:300 forAxis:UILayoutConstraintAxisHorizontal];
        
        // 会话名字 「群名称」or「联系人名称」
        self.nicknameLabel = [[UILabel alloc] init];
        self.nicknameLabel.textColor = [UIColor blackColor];
        self.nicknameLabel.font = [UIFont systemFontOfSize:17.0f];
        [self.contentView addSubview:self.nicknameLabel];
        [self.nicknameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.avatarImageView.mas_right).offset(10);
            make.top.mas_equalTo(self.avatarImageView).offset(1.5);
            make.right.mas_lessThanOrEqualTo(self.lastMsgTimeLabel.mas_left).mas_offset(-5);
        }];
        [self.nicknameLabel setContentCompressionResistancePriority:100 forAxis:UILayoutConstraintAxisHorizontal];
        
        // 最新消息内容
        self.lastMsgLabel = [[UILabel alloc] init];
        self.lastMsgLabel.textColor = [UIColor lightGrayColor];
        self.lastMsgLabel.font = [UIFont systemFontOfSize:14.0f];
        [self.contentView addSubview:self.lastMsgLabel];
        [self.lastMsgLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(self.nicknameLabel.mas_bottom).mas_offset(6);
            make.left.mas_equalTo(self.nicknameLabel.mas_left);
            make.right.mas_lessThanOrEqualTo(self.contentView);
        }];
        [self.lastMsgLabel setContentCompressionResistancePriority:110 forAxis:UILayoutConstraintAxisHorizontal];
        
        self.unReadBadge = [[UILabel alloc] init];
        self.unReadBadge.layer.backgroundColor = [UIColor redColor].CGColor;    // 红色背景
        self.unReadBadge.textColor = [UIColor whiteColor];       // 白色文字
        self.unReadBadge.font = [UIFont systemFontOfSize:11];    // 小字体
        self.unReadBadge.textAlignment = NSTextAlignmentCenter;  // 文字居中
        [self.contentView addSubview:self.unReadBadge];
        [self.unReadBadge mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(18);
            make.top.mas_equalTo(self.avatarImageView.mas_top).mas_offset(-6);
            make.right.mas_equalTo(self.avatarImageView.mas_right).mas_offset(6);
        }];
        self.unReadBadge.layer.cornerRadius = 9;  // 圆角
        self.unReadBadge.layer.masksToBounds = YES; // 裁剪超出部分
        self.unReadBadge.hidden = YES; // 默认隐藏
    }
    return self;
}

-(void)setConversation:(ConversationModel *)conversation{
    _conversation = conversation;
    [self.avatarImageView setImage:[UIImage imageNamed:@"WechatIMG5"]];
    if (conversation.unread_count) {
        self.unReadBadge.text = [NSString stringWithFormat:@"%ld", conversation.unread_count];
        self.unReadBadge.hidden = NO;
    } else self.unReadBadge.hidden = YES;
    self.nicknameLabel.text = conversation.nickname;
    self.lastMsgLabel.text = conversation.last_msg_content;
    self.lastMsgTimeLabel.text = [self formatTimeString:conversation.last_msg_time];
}

#pragma mark - Helper Methods

- (NSString *)formatTimeString:(NSInteger)timestamp {
    NSDate *date =  [NSDate dateWithTimeIntervalSince1970:timestamp];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    formatter.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
    
    // 判断是否为今天
    NSCalendar *calendar = [NSCalendar currentCalendar];
    BOOL isToday = [calendar isDateInToday:date];
    
    if (isToday) {
        // 今天显示时分
        formatter.dateFormat = @"HH:mm";
    } else if ([calendar isDateInYesterday:date]) {
        // 昨天显示"昨天"
        return @"昨天";
    } else {
        // 其他显示月日
        formatter.dateFormat = @"MM-dd";
    }
    
    return [formatter stringFromDate:date];
}

@end

